<?php
namespace app\index\controller;
class Task extends Common{
    /**
     * 获取任务菜单列表
     * @return json
     */
    function navs(){
        $navs=model('taskgroup')->readAll(['status'=>1,'phone'=>1],0,['with'=>'taskoptionsphone']);
        $prefix=request()->domain().'/taskicon/';
        $suffix='.png';
        foreach ($navs as $key => $v) {
            foreach ($v['taskoptionsphone'] as $k => $c) {
                $c['icon']=$prefix.$c['icon'].$suffix;
                if(is_string($c['params'])){
                    $c['params']=json_decode($c['params'],true);
                }
            }
            $navs[$key]['children']=$v['taskoptionsphone'];
        }
        return json(['data'=>$navs]);
    }
 


    /**
     * 获取任务列表
     * @param  integer $page 页数
     * @param  integer $tag 标签 1简单2高额3人气
     * @return [type]      [description]
     */
    function getTaskList(){
        $page=input('page/d',1);
        $size = 20;
        $start = $page*$size;

        $time = time();
        $params=[];
        $params['starttime'] = ['starttime','lt',$time];
        $params['endtime'] = ['endtime','gt',$time];

        $tag = input('tag/d');
        if(!empty($tag)){
            $where = 'FIND_IN_SET('.$tag.', tag)';
        }else{
            $where = 1;
        }

        // 标题，类型，标签，状态，奖励，已完成数，开始时间
        $field='title,type,tag,status,reward,fquantity,starttime';
        $list= model('release_task')->field($field)->where($params)->where($where)->paginate($start,$size);

        $data = $list->toArray()['data'];
        foreach ($data as &$value) {
            // 是否是简单
            $tagArr = explode(',', $value['tag']);
            $value['is_simple'] = in_array(1, $tagArr)?1:0;
            // 是否是高额
            $tagArr = explode(',', $value['tag']);
            $value['is_high'] = in_array(2, $tagArr)?1:0;
            // 是否是人气
            $tagArr = explode(',', $value['tag']);
            $value['is_popularity'] = in_array(3, $tagArr)?1:0;
        }
        // print_r($data);die;

        // echo json_encode($data);die;
        return json("",0,$data);die;
    }

    /**
     * 获取任务详情
     * @param  integer $id 用户ID
     * @return [type]      [description]
     */
    function getTaskDetails(){
        // $id = 3;
        $id=input('id/d',0);
        if(!$id){
            $this->error(lang('非法操作！'));
        }
        $params=['id'=>$id];

        // 标题，类型，标签，状态，用户可做次数，奖励，任务说明，实例图片，任务步骤，超时时间，共可做任务次数，提交次数，审核通过数，开始时间,截止时间
        $field='title,type,tag,status,limit,reward,illustrate,image_group,step,overtime,number,squantity,fquantity,starttime,endtime';
        $data= model('release_task')->where($params)->find()->toArray();

        // 是否是简单
        $tagArr = explode(',', $data['tag']);
        $data['is_simple'] = in_array(1, $tagArr)?1:0;
        // 是否是高额
        $tagArr = explode(',', $data['tag']);
        $data['is_high'] = in_array(2, $tagArr)?1:0;
        // 是否是人气
        $tagArr = explode(',', $data['tag']);
        $data['is_popularity'] = in_array(3, $tagArr)?1:0;
        
        // 解析图片组与任务步骤
        $data['image_group'] = unserialize($data['image_group']);
        foreach ($data['image_group'] as &$value) {
            $value = $_SERVER['SERVER_NAME']."/".$value;
        }
        $data['TaskStep'] = unserialize($this->mb_unserialize($data['step']));
        foreach ($data['TaskStep'] as &$value) {
            if(!empty($value['image_group'])){
                foreach ($value['image_group'] as &$v) {
                    $v['path'] = $_SERVER['SERVER_NAME']."/".$v['path'];
                }
            }
        }
        unset($data['step']);

        // 通过率计算 审核通过数/用提交数
        if($data['fquantity']>0 && $data['squantity']>0){
            $data['chance'] = $data['fquantity'] / $data['squantity'] * 100;
            $data['chance'] = round($data['chance']);
        }else{
            $data['chance'] = 0;
        }
        
        /**
            title         标题
            type          类型 1金钱 2微豆
            tag           标签
            status        状态 1上架0下架
            limit         用户可做次数 
            reward        奖励 多少钱/微豆
            illustrate    任务说明
            image_group   实例图片
            TaskStep      任务步骤
            overtime      超时时间
            number        共可做任务次数
            squantity     提交次数
            fquantity     审核通过数
            chance        通过率
            starttime     开始时间
            endtime       截止时间
            is_simple     是否是简单
            is_high       是否是高额
            is_popularity 是否是人气
        */
        // print_r($data);die;
        // echo json_encode($data);die;
        return json("",0,$data);die;

    }

    function mb_unserialize($str) {
        return preg_replace_callback('#s:(\d+):"(.*?)";#s',function($match){return 's:'.strlen($match[2]).':"'.$match[2].'";';},$str);
    }
}